package com.sdp.th.controller;


import java.math.BigDecimal;
import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.th.cache.ThCleanCache;
import com.sdp.th.db.th_eq_info.service.IThEqInfoService;
import com.sdp.th.db.th_eq_state.service.IThEqStateService;
import com.sdp.th.entity.ThEqInfo;
import com.sdp.th.entity.ThEqState;
import com.sdp.th.utils.ThConstants;

/**
 * <p>
 * 设备台账 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2025-04-12
 */
@RestController
@RequestMapping("/ThEqInfoController")
public class ThEqInfoController {

	@Autowired
	private IThEqInfoService iThEqInfoService;
	
	@Autowired
	private IThEqStateService iThEqStateService;

	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private ThCleanCache thCleanCache;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iThEqInfoService, ThEqInfo.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody ThEqInfo eq) {
		if(eq==null) {
			return Result.error("设备信息为空");
		}

		if(StringUtils.isEmpty(eq.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iThEqInfoService, ThEqInfo.class, new String[] {"deviceNo"}, "keyId", eq, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("设备编码不能重复");
			}
			eq.setDeviceState(ThConstants.设备状态_正常.getCode());
			eq.setCreateTime(LocalDateTime.now());
			iThEqInfoService.save(eq);
			//添加实时信息
			ThEqState eqState = new ThEqState();
			eqState.setDeviceNo(eq.getDeviceNo());
			eqState.setLineState(ThConstants.否.getCode());
			eqState.setAlarmState(ThConstants.否.getCode());
			eqState.setAlarmCount(BigDecimal.ZERO);
			eqState.setDeviceType(eq.getDeviceType());
			iThEqStateService.save(eqState);
		
			
			
			
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iThEqInfoService, ThEqInfo.class, new String[] {"deviceNo"}, "keyId", null, eq, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("设备编码不能重复");
			}
			iThEqInfoService.updateById(eq);
			
			//更新实时信息
			LambdaUpdateWrapper<ThEqState> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
			lambdaUpdateWrapper.set(ThEqState::getDeviceType, eq.getDeviceType());
			lambdaUpdateWrapper.eq(ThEqState::getDeviceNo, eq.getDeviceNo());
			iThEqStateService.update(lambdaUpdateWrapper);
		}

		thCleanCache.cleanEqInfoCache();
		return Result.ok();
	}
	
	
	@GetMapping(value="del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> queryEqInfoCount(@RequestParam(name="deviceNo",required=true) String deviceNo) {
		//删除基本信息
		iThEqInfoService.delEqInfoByNo(deviceNo);
		
		//删除实时监控信息
		iThEqStateService.delEqStateByNo(deviceNo);
		
		//删除缓存
		thCleanCache.cleanEqInfoCache();
		
		
		return Result.ok();
	}
}

